//
//  UserInfo.m
//  Linker
//
//  Created by hhsoft on 16/4/18.
//  Copyright © 2016年 www.huahansoft.com. All rights reserved.
//

#import "UserInfo.h"
//#import "SystemEngine.h"
#import "NSString+Valid.h"
//#import "NSString+Addtion.h"

@interface UserInfo ()
/**
 *  用户用于显示的名称 备注>昵称
 */
@property (nonatomic,copy,readwrite) NSString *userDisplayName;
@property (nonatomic,copy,readwrite) NSString *userBirthdayFull;
/**
 *  年龄，根据生日计算
 */
@property (nonatomic,assign,readwrite) NSInteger userAge;
/**
 *  性别描述
 */
@property (nonatomic,copy,readwrite) NSString *userGenderDesc;
@end

@implementation UserInfo


-(void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeInteger:_userID forKey:@"user_id"];
    [aCoder encodeObject:_userToken forKey:@"user_token"];
    [aCoder encodeObject:_userPhone forKey:@"user_phone"];
    [aCoder encodeObject:_userLinker forKey:@"user_linker"];
    [aCoder encodeObject:_userEmail forKey:@"user_email"];
    [aCoder encodeObject:_userAvatar forKey:@"user_avatar"];
    [aCoder encodeObject:_userNickName forKey:@"user_nickName"];
    [aCoder encodeObject:_userCountry forKey:@"user_country"];
    [aCoder encodeInteger:_userIsTeacher forKey:@"user_isTeacher"];
    [aCoder encodeInteger:_userIsOnline forKey:@"user_isOnline"];
    [aCoder encodeObject:_userAccount forKey:@"user_account"];
    [aCoder encodeInteger:_userGender forKey:@"user_gender"];
    [aCoder encodeObject:_userTeachingTime forKey:@"user_teachingTime"];
    [aCoder encodeObject:_userLearningTime forKey:@"user_learningTime"];
    [aCoder encodeInteger:_userTeacherNum forKey:@"user_teacherNum"];
    [aCoder encodeInteger:_userSore forKey:@"user_sore"];
    [aCoder encodeInteger:_userBirthdayYear forKey:@"user_birthdayYear"];
    [aCoder encodeInteger:_userBirthdayMonth forKey:@"user_birthdayMonth"];
    [aCoder encodeInteger:_userBirthdayDay forKey:@"user_birthdayDay"];
    [aCoder encodeObject:_nickNameFristLetter forKey:@"user_fristLetter"];
    [aCoder encodeInteger:_userLastUI forKey:@"user_lastUI"];
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
    if (self=[super init]) {
        _userID=[aDecoder decodeIntegerForKey:@"user_id"];
        _userToken=[aDecoder decodeObjectForKey:@"user_token"];
        _userPhone=[aDecoder decodeObjectForKey:@"user_phone"];
        _userLinker=[aDecoder decodeObjectForKey:@"user_linker"];
        _userEmail=[aDecoder decodeObjectForKey:@"user_email"];
        _userAvatar=[aDecoder decodeObjectForKey:@"user_avatar"];
        _userNickName=[aDecoder decodeObjectForKey:@"user_nickName"];
        _userCountry=[aDecoder decodeObjectForKey:@"user_country"];
        _userIsTeacher=[aDecoder decodeIntegerForKey:@"user_isTeacher"];
        _userGender=[aDecoder decodeIntegerForKey:@"user_gender"];
        _userIsOnline=[aDecoder decodeIntegerForKey:@"user_isOnline"];
        _userAccount=[aDecoder decodeObjectForKey:@"user_account"];
        _userTeachingTime=[aDecoder decodeObjectForKey:@"user_teachingTime"];
        _userLearningTime=[aDecoder decodeObjectForKey:@"user_learningTime"];
        _userTeacherNum=[aDecoder decodeIntegerForKey:@"user_teacherNum"];
        _userSore=[aDecoder decodeIntegerForKey:@"user_sore"];
        _userBirthdayYear=[aDecoder decodeIntegerForKey:@"user_birthdayYear"];
        _userBirthdayMonth=[aDecoder decodeIntegerForKey:@"user_birthdayMonth"];
        _userBirthdayDay=[aDecoder decodeIntegerForKey:@"user_birthdayDay"];
        _nickNameFristLetter=[aDecoder decodeObjectForKey:@"user_fristLetter"];
        _userLastUI=[aDecoder decodeIntegerForKey:@"user_lastUI"];
    }
    return self;
}
-(NSString *)userBirthdayFull{
    if (nil==_userBirthdayFull) {
        _userBirthdayFull=[NSString stringWithFormat:@"%@-%@-%@",[@(self.userBirthdayYear) stringValue],[@(self.userBirthdayMonth) stringValue],[@(self.userBirthdayDay) stringValue]];
    }
    return _userBirthdayFull;
}
-(NSInteger)userAge{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSCalendarUnitYear;
    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:[NSDate date]];
    NSInteger year = [dateComponent year];
    _userAge=year-self.userBirthdayYear;
    return _userAge;
}
-(NSString *)userGenderDesc{
    if (nil==_userGenderDesc) {
        _userGenderDesc=self.userGender==1?@"男":@"女";
    }
    return _userGenderDesc;
}
-(NSString *)userDisplayName{
    _userDisplayName=self.userRemarkName.length?self.userRemarkName:self.userNickName;
    return _userDisplayName;
}

- (NSString *)nickNameFristLetter{
    if (nil==_nickNameFristLetter) {
        _nickNameFristLetter=[_userNickName firstCharactor];
        if (nil==_nickNameFristLetter) {
            _nickNameFristLetter=@"*";
        }
    }
    return _nickNameFristLetter;
}

- (NSString *)userSignature{
    if (nil==_userSignature) {
        _userSignature=@"";
        return _userSignature;
    }
//    _userSignature=[_userSignature htmlStringFromatter];
    return _userSignature;
}
@end
